package com.jiantong.web.open.app.work;

import com.jiantong.web.core.enums.ExceptionCode;
import com.jiantong.work.model.Harmless;
import com.jiantong.work.service.HarmlessReadService;
import com.jiantong.work.service.HarmlessWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/24.
 *
 */
@Slf4j
@OpenBean
public class HarmlessApis {

    @RpcConsumer
    private HarmlessReadService harmlessReadService;

    @RpcConsumer
    private HarmlessWriteService harmlessWriteService;


    @OpenMethod(
            key = "find.harmless.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public Harmless findById(Long id){
        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Harmless> harmlessResponse = harmlessReadService.findById(id);
        if(!harmlessResponse.isSuccess()){
            log.error("failed to find harmless , cause : {}", harmlessResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    harmlessResponse.getError());
        }
        return harmlessResponse.getResult();
    }

    @OpenMethod(
            key = "paging.harmless",
            paramNames = {"pageNo","pageSize","statuses","breedFactoryId","butcherFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public Paging<Harmless> harmlessPaging(Integer pageNo, Integer pageSize,
                                           List<Integer> statuses, Long breedFactoryId,
                                           Long butcherFactoryId){
        Response<Paging<Harmless>> pagingResponse =
                harmlessReadService.pagingHarmless(pageNo,pageSize,statuses,breedFactoryId,butcherFactoryId);
        if(!pagingResponse.isSuccess()){
            log.error("failed to paging harmless , cause : {}", pagingResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

    @OpenMethod(
            key = "update.harmless",
            paramNames = {"harmless","toStatus"},
            httpMethods = RequestMethod.PUT
    )
    public Map<String , Object> updateHarmless(Harmless harmless , Integer toStatus){
        if(harmless.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Map<String , Object>> statusResponse = harmlessReadService.findStatusById(harmless.getId());
        if(statusResponse.isSuccess() && !statusResponse.getResult().get("status").equals(harmless.getStatus())){
            log.error("harmless is already submitted error = {}, cause : {}", harmless.getStatus(),statusResponse.getResult().get("status"));
            throw new OPClientException("this.report.is.already.submitted");
        }
        Response<Map<String , Object>> response = harmlessWriteService.updateHarmless(harmless,toStatus);
        if(!response.isSuccess()){
            log.error("failed to update harmless{} , cause : {}", response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "paging.farm.harmless",
            paramNames = {"pageNo","pageSize","statuses","breedFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public Paging<Harmless> farmPagingHarmless(Integer pageNo, Integer pageSize,
                                               List<Integer> statuses, Long breedFactoryId){
        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "breedFactoryId should be not null");
        }
        Response<Paging<Harmless>> pagingResponse =
                harmlessReadService.farmPagingHarmless(pageNo,pageSize,statuses,breedFactoryId);
        if(!pagingResponse.isSuccess()){
            log.error("failed to paging harmless , cause : {}", pagingResponse.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),
                    pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

    @OpenMethod(
            key = "paging.butchery.harmless",
            paramNames = {"pageNo","pageSize","statuses","butcherFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public Paging<Harmless> butcheryPagingHarmless(Integer pageNo, Integer pageSize,
                                                   List<Integer> statuses,Long butcherFactoryId){
        if(butcherFactoryId == null){
            log.error("butcherFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "butcherFactoryId should be not null");
        }
        Response<Paging<Harmless>> pagingResponse =
                harmlessReadService.butcheryPagingHarmless(pageNo,pageSize,statuses,butcherFactoryId);
        if(!pagingResponse.isSuccess()){
            log.error("failed to paging butchery harmless , cause : {}", pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }
}
